//
//  OCSSStoragePage.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/11.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit

class OCSSStoragePage: OCSSBaseViewController,UITableViewDelegate,UITableViewDataSource, OCSSStorageCellDelegate {
    
    

    @IBOutlet weak var tableView: UITableView!
    
    var listData:[NSDictionary]!;
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.regTableViewNib(tableView: tableView, className: OCSSStorageCell.className());
        initData();
    }
    func initData() -> Void {
        listData = [["icon":"content-ic-c","name":"仓库业务","titles":["申请采购","申请调拨","申请铺货","历史单据"]],
                    ["icon":"content-ic-d","name":"出库单","titles":["销售出库","批发出库","调拨出库","铺货出库","查询全部"]],
                    ["icon":"content-ic-x","name":"入库单","titles":["采购入库","调拨入库","铺货入库","退货入库","查询全部"]],
                    ["icon":"content-ic-oth","name":"其他","titles":["批量下单","求购列表","发布求购"]]];
    }
    //MARK:UITableViewDelegate && UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 4;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: OCSSStorageCell.className()) as! OCSSStorageCell;
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        if (listData != nil) {
            cell.setCellWithDict(dict: listData[indexPath.section]);
        }
    
        return cell;
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 14;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let dict:NSDictionary = listData[indexPath.section];
        let titles = dict["titles"] as! NSArray;
        
        if titles.count > 4 {
            return 168;
        }
    
        return 113;
    }
    //MARK:OCSSStorageCellDelegate
    func buttonDidClicked(button: UIButton, categoryName:String) {
        let title = button.titleLabel?.text;
        let orderDetailPage = OCSSOrderDetailPage.init();
        orderDetailPage.hidesBottomBarWhenPushed = true;
        orderDetailPage.type = orderType.outOrder;
        if (title!.hasSuffix("出库") || (title == "查询全部" && categoryName == "出库单")){
            let outputOrderPage = OCSSOutputOrderParantPage.init();
            if (button.tag - 10 != 4){
                outputOrderPage.currentIndex = button.tag-9;
            }
            outputOrderPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(outputOrderPage, animated: true);
        }else if(title!.hasSuffix("入库") || (title == "查询全部" && categoryName == "入库单")){
            let putinOrderPage = OCSSPutInOrderParentPage.init();
            if (button.tag - 10 != 4){
                putinOrderPage.currentIndex = button.tag-9;
            }
            putinOrderPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(putinOrderPage, animated: true);
            
        }else if (title! == "批量下单"){
            let bookOrderPage = OCSSOverBookingPage.init();
            bookOrderPage.title = title!;
            bookOrderPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(bookOrderPage, animated: true);
        }else if(title! .hasPrefix("申请")){
            let purchesPage = OCSSOverBookingPage.init();
            if (title! == "申请调拨"){
                purchesPage.type = BookOrderType.allot;
                purchesPage.isHasLocation = false;
            }else{
                if (title! == "申请采购"){
                    purchesPage.type = BookOrderType.purches;
                }else{
                    purchesPage.type = BookOrderType.distribution;
                }
                purchesPage.isHasLocation = true;
            }
            purchesPage.title = title!;
            purchesPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(purchesPage, animated: true);
        }else if(title! == "历史单据"){
            let historyPage = OCSSHistoryOrderParantPage.init();
            historyPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(historyPage, animated: true);
        }else if title == "发布求购"{
            let requestListPage = OCSSSelfRequestListPage.init();
            requestListPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(requestListPage, animated: true);
        }else if title == "求购列表"{
            let otherRequestPage = OCSSOtherRequestListPage.init();
            otherRequestPage.hidesBottomBarWhenPushed = true;
            navigationController?.pushViewController(otherRequestPage, animated: true);
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
